package cn.hx.mall.goods.controller;
import cn.hx.mall.goods.entity.ProductCategory;
import cn.hx.mall.goods.service.ProductCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("goods")
public class ProductSortController {
    @Resource
    ProductCategoryService productCategoryService;

    @GetMapping("productSort")
    public String queryAll(ProductCategory productCategory, Model map,Integer page,Integer size){
        if (page==null){
            page=1;
        }
        if (size==null){
            size=10;
        }
        int start=(page-1)*size;
        List<ProductCategory> productCategories = productCategoryService.queryAllWithLimit(productCategory,start,size);
        Map<String,Object> sortList=new HashMap<>();
        sortList.put("list",productCategories);

        sortList.put("pageNum" ,page);

        sortList.put("pageSize" ,size);

        int total = productCategoryService.count();
        sortList.put("total",total);

        int pages;
        if (total!=0&&total%size==0){
            pages=total/size;
        }else {
            pages=(int) (1+Math.floor((double)total/size));
        }

        sortList.put("pages" ,pages);

        map.addAttribute("sortList",sortList);
        return "goods/sort";
    }

}
